//################################################################################################################################//
//##                                                                                                                            ##//
//## This User Control is implement for dentist endorsement                                                                     ##//
//##                                                                                                                            ##//
//## Author   : Kajal                                                                                                           ##//
//## Dated    : 28-07-2007                                                                                                      ##//
//##                                                                                                                            ##//
//################################################################################################################################//

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Data.SqlClient;

public partial class includes_DntEndorsementRating : System.Web.UI.UserControl, ICallbackEventHandler 
{   
    string _RatingType = "";
    int _dentistId = 0;
    public int FullRate = 0;
	public bool halfRate = false;        
    string _imageName = "plus";
    
    /// <summary>
    /// Name of Rating field In DataBase
    /// </summary>
    public string RatingType
    {
        set { _RatingType = value; }
        get { return _RatingType; }
    }
    /// <summary>
    /// hold Current Dentist ID
    /// </summary>
    public int DentistId
    {
        set { _dentistId = value; }
        get { return _dentistId; }
    }
    public string imageName
    {
        set { _imageName = value; }
        get { return _imageName; }
    }
    decimal _rating=0.00M;
    public decimal rating
    {
        set { _rating = value; }
        get { return _rating; }
    }
        
    decimal _totalScore = 5;
    public decimal totalScore
    {
        set { _totalScore = value; }
        get { return _totalScore; }
    }

    bool _isDentist = false;
    public bool isDentist
    {
        set { _isDentist = value; }
        get { return _isDentist; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {        
        //set Java Script for Client Call back handling
        if (ViewState["DoctorId"] == null || ViewState["RatingType"] == null)
        {
            ViewState["DoctorId"] = this.DentistId;
            ViewState["RatingType"] = this.RatingType;
        }
        else
        {
            this.DentistId = Convert.ToInt32(ViewState["DoctorId"]);
            this.RatingType = ViewState["RatingType"].ToString();
        }
        
        ClientScriptManager cm = Page.ClientScript;
        String cbReference = cm.GetCallbackEventReference(this, "arg", "ReceiveServerData" + this.ID, "");
        String callbackScript = "function CallServer" + this.ID + "(arg) {" + cbReference + "; }";
        cm.RegisterClientScriptBlock(this.GetType(), "CallServer" + this.ID + "(arg)", callbackScript, true);
        
            //read current rating from database  userType                      
        getRating();        
    }
    protected void getRating()
    {
        if (this.rating > this.totalScore)
            this.rating = totalScore;
        decimal rate = this.rating/(this.totalScore/5);
       
        FullRate = (int)Math.Round(rate, 0);        
        //FullRate  =(int)Math.Truncate(rate); //cR;		
        //if ((Math.Round((Math.Round(rate, 1) - FullRate), 1) >= .3M && (Math.Round((Math.Round(rate, 1) - FullRate), 1)) <= .7M))
        //    halfRate = true; 
        //if ((Math.Round((Math.Round(rate, 1) - FullRate), 1)) > .7M)
        //FullRate += 1;        
    }

     #region ICallbackEventHandler Members
    string userLastRating ="0";
    public string GetCallbackResult()
    {
        return userLastRating; //getRating().ToString();
    }
    

    public void RaiseCallbackEvent(string eventArgument)
    {   ///handle callback insert new rating in database

       
        decimal rateByUser = Convert.ToDecimal(eventArgument)*(this.totalScore/5);
        userLastRating = rateByUser.ToString(); 
          // Database  code
        try
        {
            SqlParameter[] sqlParam = new SqlParameter[5];
            sqlParam[0] = new SqlParameter("@DntId", SqlDbType.Int);
            sqlParam[1] = new SqlParameter("@UsrId", SqlDbType.Int);
            sqlParam[2] = new SqlParameter("@RatingType", SqlDbType.VarChar, 100);
            sqlParam[3] = new SqlParameter("@ratevalue", SqlDbType.Decimal);
            sqlParam[3].Precision = 18;
            sqlParam[3].Scale = 2;
            sqlParam[4] = new SqlParameter("@IsDentist", SqlDbType.Bit);
            
            sqlParam[0].Value = _dentistId;
            sqlParam[1].Value = Convert.ToInt32(Session["userID"].ToString());
            sqlParam[2].Value = this.RatingType;
            sqlParam[3].Value = rateByUser;
            sqlParam[4].Value = _isDentist;

            Manager mgr = new Manager(true,"ConnectionString");
            //userLastRating = "insert_patientreview " + sqlParam[0].Value + "," + sqlParam[1].Value + "," + sqlParam[2].Value + "," + sqlParam[3].Value + "," + sqlParam[4].Value;
            mgr.ExecuteUpdate("InsertDentistReviews", sqlParam);
            
        }
        catch (Exception ex)
        {
            userLastRating=ex.ToString();
        }
    }

    #endregion
}
